package com.melody.android.logic.util

import android.annotation.SuppressLint
import android.content.Context
import com.melody.android.logic.model.Song

/**
 * 存储登录状态
 */
@SuppressLint("CommitPrefEdits")
fun storageLoginState(context: Context) {
    val sharedPreferences = context.getSharedPreferences("User", Context.MODE_PRIVATE).edit()
    sharedPreferences.putBoolean("loginState", true)
    sharedPreferences.apply()
}

/**
 * 获取登录状态
 */
fun getLoginState(context: Context): Boolean {
    val sharedPreferences = context.getSharedPreferences("User", Context.MODE_PRIVATE)
    return sharedPreferences.getBoolean("loginState", false)
}

/**
 * 存储当前选中的歌曲
 */
@SuppressLint("CommitPrefEdits")
fun storageSelectedSong(context: Context, song: Song) {
    val sharedPreferences = context.getSharedPreferences("Song", Context.MODE_PRIVATE).edit()
    sharedPreferences.putInt("songCover", song.cover)       // 歌曲封面
    sharedPreferences.putString("songName", song.name)      // 歌曲名称
    sharedPreferences.putString("songAuthor", song.author)  // 歌曲作者
    sharedPreferences.apply()
}

/**
 * 获取当前选中的歌曲
 */
fun getSelectedSong(context: Context): Song {
    val sharedPreferences = context.getSharedPreferences("Song", Context.MODE_PRIVATE)
    val songCover = sharedPreferences.getInt("songCover", 0)
    val songName = sharedPreferences.getString("songName", "")
    val songAuthor = sharedPreferences.getString("songAuthor", "")

    return Song(songCover, songName.toString(), songAuthor.toString())
}